package com.example.liuhb.h5;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.example.liuhb.R;

public class MainActivity extends Activity implements View.OnKeyListener,View.OnClickListener{
    WebView webview;
    ProgressBar progressBar;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webview = (WebView) findViewById(R.id.webView);
        progressBar= (ProgressBar) findViewById(R.id.progress_bar);
//        WebSettings wv_setttig = webview.getSettings();
//        wv_setttig.setJavaScriptEnabled(true);
//        // wv_setttig.setRenderPriority(RenderPriority.HIGH);


        WebSettings webSettings = webview.getSettings();
        //设置支持javascript
        webSettings.setJavaScriptEnabled(true);
        //扩大比例的缩放
        webSettings.setUseWideViewPort(true);
        //WebView屏幕自适应
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webSettings.setLoadWithOverviewMode(true);
        // 设置可以支持缩放
        webSettings.setSupportZoom(true);
       // 设置出现缩放工具
        webSettings.setBuiltInZoomControls(true);

        //设置不访问默认浏览器
//        webview.setWebViewClient(new WebViewClient(){
//            @Override
//            //这个方法，在web页面打开链接时，自动调用android自带的浏览器打开链接
//            public boolean shouldOverrideUrlLoading(WebView view, String url){
//                return false;
//            }
//        });
//        webview.loadUrl("http://www.baidu.com");
//          String url="http://www.baidu.com";
//        String url = "file:///android_asset/login.html";
//        String url = "http://192.168.8.35:8889/firstWeb/login.html";
        String url = "http://192.168.8.35/BDS/login.html";
        //WebViewClient类 作用：处理各种通知 和请求事件
        webview.setWebViewClient(new HelloWebViewClient());
        //将MyObject对象暴露给js脚本
        webview.addJavascriptInterface(new MyObject(this),"MyObject");
//        webview.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                switch (event.getAction()) {
//                    case MotionEvent.ACTION_DOWN:
//                    case MotionEvent.ACTION_UP:
//                        if (!v.hasFocus()) {
//                            v.requestFocus();
//                        }
//                        break;
//                }
//                return false;
//            }
//        });
        webview.loadUrl(url);
        webview.setOnKeyListener(this);
        webview.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if(newProgress==100){
                    progressBar.setVisibility(View.GONE);//加载完网页进度条消失
                }else {
                    progressBar.setVisibility(View.VISIBLE);//开始加载网页时显示进度条
                    progressBar.setProgress(newProgress);//设置进度值
                }
                super.onProgressChanged(view, newProgress);
            }
        });

    }

    @Override
    public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
        if (keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
            if (keyCode == KeyEvent.KEYCODE_BACK && webview.canGoBack()) {
                //表示按返回键时的操作
                webview.goBack();   //后退
                //webview.goForward();//前进
                return true;    //已处理
            }
        }
        return false;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn:
                Intent intent=new Intent();
                intent.setClass(MainActivity.this,WelcomeActivity.class);
                startActivity(intent);
                break;
        }
    }

    //Web视图
    private class HelloWebViewClient extends WebViewClient {

        @Override
        //这个方法，在web页面打开链接时，自动调用android自带的浏览器打开链接
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            System.out.println("url:"+url);
            //Toast.makeText(MainActivity.this, "url:"+url, Toast.LENGTH_SHORT).show();
            if (url.contains("/android")) {
                Intent intent=new Intent();
                intent.setClass(MainActivity.this,WelcomeActivity.class);
                startActivity(intent);
                return true;
            }
            return super.shouldOverrideUrlLoading(view, url);
//            view.loadUrl(url);
//            return true;
        }
    }

}
